package com.example.healthmanagement.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.healthmanagement.R;
import com.example.healthmanagement.adapter.SportRecordAdapter;
import com.example.healthmanagement.bean.SportRecord;
import com.example.healthmanagement.util.MySqliteOpenHelper;
import com.example.healthmanagement.util.SPUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * 打卡记录
 */
public class SportRecordListActivity extends AppCompatActivity {
    MySqliteOpenHelper helper = null;
    private Activity myActivity;//上下文
    private RecyclerView rvList;
    private SportRecordAdapter sportRecordAdapter;
    private LinearLayout llEmpty;
    private List<SportRecord> sportRecordList;
    private SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
    private SimpleDateFormat sf1 = new SimpleDateFormat("HH:mm:ss");
    private Integer userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sport_record_list);
        myActivity = this;
        helper = new MySqliteOpenHelper(this);
        llEmpty = findViewById(R.id.ll_empty);
        rvList = findViewById(R.id.rv_list);
        //获取控件
        userId = (Integer) SPUtils.get(SportRecordListActivity.this, SPUtils.USER_ID, 0);
        initView();
    }

    /**
     * 初始化页面
     */
    private void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(myActivity);
        //=1.2、设置为垂直排列，用setOrientation方法设置(默认为垂直布局)
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        //=1.3、设置recyclerView的布局管理器
        rvList.setLayoutManager(layoutManager);
        //==2、实例化适配器
        //=2.1、初始化适配器
        sportRecordAdapter = new SportRecordAdapter();
        //=2.3、设置recyclerView的适配器
        rvList.setAdapter(sportRecordAdapter);
        sportRecordAdapter.setItemListener(new SportRecordAdapter.ItemListener() {
            @Override
            public void ItemClick(SportRecord sportRecord) {
            }

            @Override
            public void ItemLongClick(SportRecord sportRecord) {

            }
        });
        loadData();
    }

    private void loadData() {
        sportRecordList = new ArrayList<>();
        SportRecord sportRecord = null;
        String sql = "select * from sport_record where userId = " + userId +" order by date";
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.rawQuery(sql, null);
        if (cursor != null && cursor.getColumnCount() > 0) {
            while (cursor.moveToNext()) {
                Integer dbId = cursor.getInt(0);
                Integer dbUserId = cursor.getInt(1);
                Integer step = cursor.getInt(2);
                String remark = cursor.getString(3);
                String date = cursor.getString(4);
                String time = cursor.getString(5);
                sportRecord = new SportRecord(dbId, dbUserId, step,remark, date, time);
                sportRecordList.add(sportRecord);
            }
        }
        Collections.reverse(sportRecordList);
        if (sportRecordList != null && sportRecordList.size() > 0) {
            rvList.setVisibility(View.VISIBLE);
            llEmpty.setVisibility(View.GONE);
            sportRecordAdapter.addItem(sportRecordList);
        } else {
            rvList.setVisibility(View.GONE);
            llEmpty.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            loadData();//加载数据
        }
    }

    public void back(View view) {
        finish();
    }
}
